package com.example.listener_demo.slice;

import com.example.listener_demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener{
    Text txt1 = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        DirectionalLayout directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_dl);
        txt1 = (Text) findComponentById(ResourceTable.Id_tt);
        directionalLayout.setTouchEventListener((Component.TouchEventListener) this);
    }

    float startX = 0;
    float startY = 0;

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        int action = touchEvent.getAction();
        if (action == TouchEvent.PRIMARY_POINT_DOWN) {
            MmiPoint position = touchEvent.getPointerPosition(0);
            startX = position.getX();
            startY = position.getY();
        } else if (action == TouchEvent.POINT_MOVE) {
            MmiPoint position = touchEvent.getPointerPosition(0);
        } else if (action == TouchEvent.PRIMARY_POINT_UP) {
            MmiPoint position = touchEvent.getPointerPosition(0);
            float endX = position.getX();
            float endY = position.getY();
            if (endX > startX && Math.abs(endY - startY) < 1000) {
                txt1.setText(("右滑"));
            } else if (endX < startX && Math.abs(endY - startY) < 1000) {
                txt1.setText(("左滑"));
            } else if (endY > startY && Math.abs(endX - startX) < 1000) {
                txt1.setText(("下滑"));
            } else if (endY < startY && Math.abs(endX - startX) < 1000) {
                txt1.setText(("上滑"));
            }
        }
        return true;
    }
}
